package cn.rui.common.aspects;

import cn.rui.common.utils.IpUtiles;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Enumeration;

/**
 * 切面类,启动类上加  @EnableAspectJAutoProxy注解
 * @author 徽州大都督
 * @date 2020/8/14
 */
@Aspect
@Component
public class MyAspects {

    //日志
    private static Logger logger = LoggerFactory.getLogger (MyAspects.class);

    //定义切入点在controller下面的所有类所有方法任意参数
    @Pointcut("execution(* cn.rui.controller.core.*.*(..))")
    public void weblog(){

    }

    //前置通知
    @Before ("weblog()")
    public void myBefore(JoinPoint joinPoint) throws NoSuchMethodException {

        Object target = joinPoint.getTarget();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = target.getClass().getMethod(signature.getName(), signature.getParameterTypes());

        Log annotation = method.getAnnotation(Log.class);
        if (annotation!=null){
            //controll层@Log注解的内容
            logger.info ("content:{}",annotation.value ());
        }

        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes ();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;

        HttpServletRequest request = servletRequestAttributes.getRequest ();

        //获取security保存的用户信息
        //Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        //当前登录用户
        //logger.info ("username:{}",request.getSession().getAttribute("username"));

        //请求路径
        logger.info ("URL:{}",request.getRequestURI ());
        //请求方式
        logger.info ("METHOD:{}",request.getMethod ());
        //请求地址
        //logger.info ("ADDRESS:{}",request.getRemoteAddr ());
        logger.info ("ADDRESS:{}", IpUtiles.getRealIp(request));


        //请求参数
        Enumeration<String> parameterNames = request.getParameterNames ();
        while (parameterNames.hasMoreElements ()){
            String name = parameterNames.nextElement ();
            logger.info ("name:{},value:{]",name,request.getParameter (name)); //返回?name=lidazhu的键和值
        }
    }

    //后置通知
    @AfterReturning(returning = "ret",pointcut = "weblog()")
    public void houzhi(String ret){
        //请求参数
        logger.info ("Return:{}",ret);  //返回haha
    }

    //异常通知
    @AfterThrowing(throwing="ex",pointcut ="weblog()")
    public void afterThrowing(JoinPoint joinPoint, Exception ex) {
        String methodName = joinPoint.getSignature ().getName ();
        System.out.println ("异常方法: " + methodName + " : " + ex);
    }
}
